
import * as actionTypes from '../action-types';
//定义一个动作创建者对象
const actionCreators = {
    add1() {
        return {
            type: actionTypes.ADD1
        }
    },
    minus1() {
        return {
            type: actionTypes.MINUS1
        }
    },
    thunkAdd() {
        //此处的dispatch是改造后的dispatch，而非最原始的store.dispatch
        return function (dispatch, getState) {
            setTimeout(() => {
                dispatch(function (dispatch, getState) {
                    setTimeout(() => {
                        dispatch({ type: actionTypes.ADD1 })
                    }, 1000);
                })
            }, 1000);
        }
    },
    promiseAdd() {
        return new Promise((resolve) => {
            setTimeout(
                () => resolve({ type: actionTypes.ADD1 })
                , 1000);
        });
    },
    promise2Add() {
        return {
            type: actionTypes.ADD1,
            payload: new Promise((resolve,reject) => {
                setTimeout(()=>{
                    let number = Math.random();
                    if(number>.5){
                        resolve(number);
                    }else{
                        reject(number);
                    }
                }, 1000);
            })
        }
    }
}
export default actionCreators;